package cn.hallele.bbts.crawler.api.controller;

import cn.hallele.bbts.core.common.annotations.Log;
import cn.hallele.bbts.core.common.enums.LogTypeEnum;
import cn.hallele.bbts.core.common.utils.BeanUtil;
import cn.hallele.bbts.core.component.io.BBTSPage;
import cn.hallele.bbts.core.component.io.BBTSRequest;
import cn.hallele.bbts.core.component.io.BBTSResponse;
import cn.hallele.bbts.crawler.api.dto.LotteryDataDTO;
import cn.hallele.bbts.crawler.common.constants.LogSubTypeConstant;
import cn.hallele.bbts.crawler.service.actions.unionlotto.UnionLottoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * UnionLottoController
 *
 * @author anle5
 * @since 2023/12/3 星期日 13:02
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("UnionLotto")
@Tag(name = "UnionLotto", description = "双色球爬虫接口")
public class UnionLottoController {
    private final UnionLottoService unionLottoService;

    /**
     * 启动UnionLotto爬虫
     *
     * @return 响应
     */
    @Log(type = LogTypeEnum.TASK, subType = LogSubTypeConstant.TASK_UNION_LOTTO_START)
    @GetMapping("start/{pageNo}/{pageSize}")
    @Operation(description = "启动双色球爬虫")
    public BBTSResponse<String> start(
            @PathVariable("pageNo") @Min(value = 1) int pageNo,
            @PathVariable("pageSize") @Min(value = 1) int pageSize) {
        unionLottoService.start(pageNo, pageSize);
        return BBTSResponse.ok("UnionLotto");
    }

    /**
     * 启动UnionLotto爬虫
     *
     * @return 响应
     */
    @Log(type = LogTypeEnum.QUERY, subType = LogSubTypeConstant.QUERY_UNION_LOTTO)
    @GetMapping("query/{pageNo}/{pageSize}")
    @Operation(description = "查询双色球数据")
    public BBTSResponse<BBTSPage<List<LotteryDataDTO>>> query(
            @PathVariable("pageNo") @Min(value = 1) int pageNo,
            @PathVariable("pageSize") @Min(value = 1) int pageSize) {
        BBTSPage<List<?>> result = unionLottoService.query(pageNo, pageSize);
        return BBTSResponse.ok(BeanUtil.transferPage(result, LotteryDataDTO.class));
    }

    /**
     * 测试
     *
     * @return 响应
     */
    @Log(type = LogTypeEnum.OTHER, subType = LogSubTypeConstant.QUERY_UNION_LOTTO)
    @PostMapping("add")
    @Operation(description = "查询双色球数据")
    public BBTSResponse<String> add(@RequestBody @Valid BBTSRequest<LotteryDataDTO> request) {
        return BBTSResponse.ok(request.getBody().getIssueId());
    }
}
